package com.hry.springboot.source.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.EventListener;
import java.util.concurrent.TimeUnit;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/4/26
 **/
@Component
public class MyListener  implements SmartLifecycle, DisposableBean, ApplicationContextAware  {

    private ApplicationContext applicationContext;

    @Override
    public void start()  {
        System.out.println("start");
        while (true) {
//            try {
////                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            System.out.println(applicationContext.getBean(MyListener.class));
        }
    }

    @Override
    public void stop() {
        System.out.println("stop");
    }

    @Override
    public boolean isRunning() {
        System.out.println("isRunning");
        return false;
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
